home *** CD-ROM | disk | FTP | other *** search
/ Sun Solutions 1997 April to September / Sun Solutions CD - APR '97 - SEP '97 (704-3778-12 Rev. H)(Sun Microsystems, Inc.)(1997).iso / products / Versant / stockquote.pl < prev    next >
Perl Script  |  1997-01-08  |  6KB  |  193 lines

  1. #!/usr/local/bin/perl
  2.  
  3. $stkdir = "/www/versant/versant.v1";
  4. chdir $stkdir;
  5.  
  6. $filesrc="tmpl.htm";   # This is the file you can edit.
  7. $fileorig="stock.org";       # This is a semi-pristine copy.
  8. $filedest="stock.htm";       # This is the result.
  9.  
  10. $quote = "vsnt";
  11. #print "Getting info\n";
  12. $stock = `/www/versant/quote/quote $quote`;
  13.  
  14. ($Symbol,$Date,$Time,$Open,$High,$Low,$Last,$Change,$Volume) = split(/\t/,$stock);
  15. ($Market,$Symbol) = split (/:/, $Symbol);
  16. $Prev = $Last - $Change;
  17.  
  18. $Last = &makefraction ($Last);
  19. $Prev = &makefraction ($Prev);
  20. $Open = &makefraction ($Open);
  21. $High = &makefraction ($High);
  22. $Low = &makefraction ($Low);
  23. $Change = &makefraction ($Change);
  24. $Volume = &makefraction ($Volume);
  25.  
  26. #print "Making Table\n";
  27. $table = "<TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0><TR><TD>
  28.         <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=5 BGCOLOR=9a9a9a>
  29.         <TR>
  30.             <TH ALIGN=left><FONT COLOR=ffffff SIZE=-1>Symbol</FONT></TH>
  31.             <TH ALIGN=CENTER><FONT COLOR=ffffff SIZE=-1>Last</FONT></TH>
  32.             <TH ALIGN=CENTER><FONT COLOR=ffffff SIZE=-1>Change</FONT></TH>
  33.             <TH ALIGN=CENTER><FONT COLOR=ffffff SIZE=-1>Previous</FONT></TH>
  34.             <TH ALIGN=CENTER><FONT COLOR=ffffff SIZE=-1>Low</FONT></TH>
  35.             <TH ALIGN=CENTER><FONT COLOR=ffffff SIZE=-1>High</FONT></TH>
  36.             <TH ALIGN=right><FONT COLOR=ffffff SIZE=-1>Volume</FONT></TH>
  37.         </TR>
  38.         <TR BGCOLOR=6699CC>  
  39.             <TD ALIGN=LEFT><B><FONT FACE=ARIAL,HELVETICA COLOR=000000 SIZE=-1>$Symbol</FONT></B></TD>
  40.             <TD ALIGN=CENTER><FONT FACE=ARIAL,HELVETICA COLOR=000000 SIZE=-1>$Last</FONT></TD>
  41.             <TD ALIGN=CENTER><FONT FACE=ARIAL,HELVETICA COLOR=red SIZE=-1>$Change</FONT></TD>
  42.             <TD ALIGN=CENTER><FONT FACE=ARIAL,HELVETICA COLOR=000000 SIZE=-1>$Prev</FONT></TD>
  43.             <TD ALIGN=CENTER><FONT FACE=ARIAL,HELVETICA COLOR=000000 SIZE=-1>$Low</FONT></TD>
  44.             <TD ALIGN=CENTER><B><FONT FACE=ARIAL,HELVETICA COLOR=000000 SIZE=-1>$High</FONT></B></TD>
  45.             <TD ALIGN=RIGHT><FONT FACE=ARIAL,HELVETICA COLOR=000000 SIZE=-1>$Volume</FONT></TD>
  46.         </TR>
  47.  
  48.         </TABLE>
  49.     </TD></TR>
  50.     </TABLE>
  51.     </CENTER>";
  52.  
  53. $sof = "$Date @ $Time ET";
  54.  
  55. #print "Updating File\n";
  56. &updatefile ($table,$sof,1);
  57.  
  58. sub updatefile {
  59.     local($t,$d,$s) = ($_[0],$_[1],$_[2]);
  60.     local($intab,$nocmt,$copyback)=(0,0,0);
  61.  
  62. # Work with a safe 'original' copy of the file.
  63. # This copy doesn't get modified by overwriting, limiting mutation.
  64.  
  65.     if ( ! -e $fileorig ) {
  66.         if ( ! -e $filesrc ) {
  67.             return -1;
  68.         } else {
  69.             ©file ($filesrc, $fileorig);
  70.         }
  71.     } elsif ( -e $filesrc &&  (  -M $fileorig >  -M $filesrc  ) ){
  72.         ©file ($filesrc, $fileorig);
  73.     }
  74.  
  75. # If we don't have a target file yet, at all, use the original.
  76.     if ( $s <= 0 ) {
  77.         if ( ! -e $filedest ) {
  78.             ©file ($fileorig, $filedest);
  79.         } elsif ( -e $fileorig &&  ( -M $filedest > -M $fileorig ) ){
  80.             ©file ($fileorig, $filedest);
  81.         }
  82.         return -2;
  83.     }
  84.  
  85.     open (HTML,"<" . $fileorig);
  86.     open (NHTML,">" . $filedest);
  87.  
  88.     $ret = "\r";                # For when it's needed.  Puzzling to me.
  89.     $ret = "\n";                # For when it's needed.  Puzzling to me.
  90.     $\ = "";                    # This could be made "\r" to be more
  91.  
  92.     while (<HTML>) {
  93.         if ( $nocmt == 0 && m/<!-- Okay, you can change stuff after/io ) {
  94.             $intab = 2 ;
  95.         }
  96.  
  97.         if ( $usefallback == 1 && $intab == 0 && m/<table/io) {
  98.             $intab == 0;
  99.         } elsif ( $intab != 1) {
  100.  
  101.             # Normal copy behavior.
  102.  
  103.             print NHTML $_;
  104.         }
  105.  
  106.         if ( $nocmt == 1 && m/<!-- Okay, you can change stuff after/io ) {
  107.             # This is abnormal; orig has '<!--' ending comment only.
  108.             $intab = 4 ;
  109.         }
  110.  
  111.         if ( m/<!-- Do not change: table is auto-inserted/io ) {
  112.             # This is normal; orig has '<!--' comments.
  113.             $intab = 1 ;
  114.             if ($s == 1) {      # got as "table" succeeded
  115.                 &dumptable($t,$d,$ret);
  116.             }
  117.             $copyback=0;
  118.         }
  119.  
  120.         if ( $intab == 1 && $s == 2 ) {
  121.             &evalline($_);
  122.         }
  123.  
  124.         if ($nocmt == 1 && $intab == 1 && m/As of:/io) {
  125.             # This is acceptable; orig has no opening '<!--' comment.
  126.             $intab = 3;
  127.         }
  128.  
  129.         if ( $usefallback == 1 && $intab == 0 && m/<table/io) {
  130.             # This is acceptable if $usefallback == 1;
  131.             #   orig has no opening '<!--' comment.
  132.             $nocmt = 1;
  133.             $intab = 1;
  134.  
  135.             print NHTML "<!-- Do not change: table is auto-inserted-->" . $ret;
  136.             &dumptable($t,$d,$ret);         
  137.             print NHTML "<!-- Okay, you can change stuff after this-->" . $ret;
  138.             $copyback=1;
  139.         }
  140.     }
  141.  
  142.     close HTML;
  143.     close NHTML;
  144.  
  145.     if ($usefallback == 1 &&  $copyback == 1) {# Original needs '<!--'
  146.                 # comments, so copy file.
  147.         ©file ($fileorig,"C" . $fileorig);
  148.         ©file ($filedest, $fileorig);
  149.     }
  150.     return 0;
  151. }
  152.  
  153. sub dumptable {
  154.     local($t,$d,$ret) = ($_[0],$_[1],$_[2]);
  155.     print NHTML $t . $ret ;print NHTML "";
  156.     print NHTML "<center>" . $ret ;print NHTML "";
  157.     print NHTML "<br>" . $ret ;print NHTML "";
  158.     print NHTML "<font size=2><i>Stock price as of: " . $d . "</i></font></center>" . $ret ;print NHTML "";
  159. }
  160.  
  161. sub makefraction {
  162.     local ($int,$dec) = split (/\./,$_[0]);
  163.     local $num;
  164.     local $bot;
  165.     $bot =10000;
  166.     if ($dec != 0) {
  167.         while ($dec < 1000) { $dec *= 10; }
  168.         while ($dec%5 == 0 && $bot%5 == 0 ) {
  169.             $bot /= 5;
  170.             $dec /= 5;
  171.         }
  172.         while ($dec%2 == 0 && $bot%2 == 0) {
  173.             $bot /= 2;
  174.             $dec /= 2;
  175.         }
  176.         $num = $int." ".$dec."/".$bot;
  177.     } else { $num = $int; }
  178. #    print "$num\n";
  179.     $num;
  180.  
  181. }
  182. sub copyfile {
  183.     local ($src,$dest) = ($_[0],$_[1]);
  184.     open (SRC,"<$src") || return -1;
  185.     open (DEST,">$dest") || return -2;
  186.     while (<SRC>) {
  187.         printf DEST "%s",$_;
  188.     }
  189.     close SRC;
  190.     close DEST;
  191.     return 0;
  192. }
  193.